import jmespath

from config_params.base_util import baseUtil
from jmeter_api.configs.http_request_defaults.elements import Argument


class ConfigElement:
    def __init__(self, data):
        self.data = data

    def jme_path(self, exp):
        return jmespath.search(exp, self.data)

    def HTTPCacheManager(self, config_obj):
        http_cache__manager_dict = {}
        http_cache__manager_dict["name"] = self.jme_path("name") if self.jme_path("name") is not None else "HTTP_Cache_Manager"
        http_cache__manager_dict["comments"] = self.jme_path("comments") if self.jme_path("comments") is not None else ""
        http_cache__manager_dict["clear_each_iteration"] = self.jme_path("clear_each_iteration") if self.jme_path("clear_each_iteration") is not None else False
        http_cache__manager_dict["use_cache_control"] = self.jme_path("use_cache_control") if self.jme_path("use_cache_control") is not None else True
        http_cache__manager_dict["max_elements_in_cache"] = int(self.jme_path("max_elements_in_cache")) if self.jme_path("max_elements_in_cache") is not None else 300
        http_cache__manager_dict["is_enabled"] = self.jme_path("is_enabled") if self.jme_path("is_enabled") is not None else True
        http_cache__manager_obj = baseUtil.create_instance(config_obj, http_cache__manager_dict)
        return http_cache__manager_obj

    def UserDefineVariables(self, config_obj):
        user_defined_variables = {}
        user_defined_variables["name"] = self.jme_path("name") if self.jme_path("name") is not None else "User Defined Variables"
        user_defined_variables["comments"] = self.jme_path("comments") if self.jme_path("comments") is not None else ""
        arguments = self.jme_path("arguments")
        arguments_new_list = []
        for argument in arguments:
            new_arguments = {}
            new_arguments["name"] = argument["name"] if argument["name"] is not None else ""
            new_arguments["value"] = str(argument["value"]) if argument["value"] is not None else ""
            new_arguments["desc"] = argument["desc"] if argument["desc"] is not None else ""
            new_arguments["metadata"] = argument["metadata"] if argument["metadata"] is not None else "="
            arguments_new_list.append(new_arguments)

        user_defined_variables["arguments"] = [baseUtil.create_instance("Argument", argument) for argument in arguments_new_list]
        user_defined_variables["is_enabled"] = self.jme_path("is_enabled") if self.jme_path("is_enabled") is not None else True
        user_defined_obj = baseUtil.create_instance(config_obj, user_defined_variables)
        return user_defined_obj
    def configElement(self):
        config_obj = self.jme_path("type")
        config_obj_list = []
        if config_obj == "HTTPCacheManager":
            config_element_obj = self.HTTPCacheManager(config_obj)
            config_obj_list.append(config_element_obj)
        elif config_obj == "UserDefineVariables":
            config_element_obj = self.UserDefineVariables(config_obj)
            config_obj_list.append(config_element_obj)
        return config_obj_list